#region File Description
//-----------------------------------------------------------------------------
// GameplayScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using AIniciacao.Lib.Screens;
using AIniciacao.Lib.Input;
using AIniciacao.Lib;
using System.Collections.Generic;
using Module_Persistence;
using Module_Basic_Entities;
using AIniciacao;
using AIniciacao.Lib.GameUtils;
using AIniciacao.MenuScreens;


#endregion

namespace AIniciacao.Screens
{
    /// <summary>
    /// This screen implements the actual game logic. It is just a
    /// placeholder to get the idea across: you'll probably want to
    /// put some more interesting gameplay in here!
    /// </summary>
    class GameplayScreen : GameScreen
    {
        #region Fields
        ContentManager content;
        SpriteFont gameFont;
        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public GameplayScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
            World.GameState = GameState.GAME;
        }

        #endregion


        public void checkCutScenes()
        {
            /*
            switch (World.GameState)
            {
                    
                case GameState.NEWYORKCUTSCENE:
                    if (World.CurrentCutSceneScreen == null)
                    {
                        World.LoadCutSceneScreen(GameConstants.NewYorkCutSceneIndex);
                        ScreenManager.AddScreen(World.CurrentCutSceneScreen,null);
                    }
                    else if(World.CurrentCutSceneScreen.IntroFinished)
                    {
                        World.GameState = GameState.BEGINCUTSCENE;
                        ScreenManager.RemoveScreen(World.CurrentCutSceneScreen);
                        World.CurrentCutSceneScreen = null;
                    }

                break;
                case GameState.BEGINCUTSCENE:
                    if (World.CurrentCutSceneScreen == null)
                    {
                        World.LoadCutSceneScreen(GameConstants.BeginCutSceneIndex);
                        ScreenManager.AddScreen(World.CurrentCutSceneScreen, null);
                    }
                    else if (World.CurrentCutSceneScreen.IntroFinished)
                    {
                        World.GameState = GameState.GAME;
                    }
                      
                 break;
                default:
                if (World.CurrentCutSceneScreen != null)
                {
                    ScreenManager.RemoveScreen(World.CurrentCutSceneScreen);
                    World.CurrentCutSceneScreen = null;
                    World.GameState = GameState.GAME;
                }
                break;
                     
            }*/

        }
        public void checkTriggers()
        {
            Tile? intersectedTile = World.getCurrentTile();
            if (intersectedTile.Value.Triggable)
            {   
                World.Triggers[intersectedTile.Value.TriggerType].Update(); 
            }
        }

        public void checkCollisions()
        {
            Tile? intersectedTile = World.getCurrentTile();
            if (!intersectedTile.Value.Walkable)
            {
                World.Detetive.LastMovement();
            }
        }

        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, GameConstants.Content);

            gameFont = content.Load<SpriteFont>(GamePathObjects.GameFontPath); 
            World.SpriteBatch = ScreenManager.SpriteBatch;
            World.Content = content;
            World.LoadHealthBar();
            World.LoadKeysBar() ;
            World.LoadDetective();
            World.LoadMap(GamePathObjects.MapAIniciacaoPath,GamePathObjects.AnimMapAIniciacaoPath);
            World.LoadCutScenes();
            World.LoadTriggers();
            World.Time = new WorldTime(5,0);
            World.LoadSounds();
            World.GraphicsDevice = ScreenManager.GraphicsDevice;
            ScreenManager.Game.ResetElapsedTime();

        }
        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }
        #region Update and Draw


        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            checkCutScenes();
             if(World.GameState == GameState.GAME)
               {
                    if (World.Time.IsTimeUp && !World.Time.TimeHandled)
                        ScreenManager.AddScreen(new GameOverMenuScreen(), ControllingPlayer);
                    if (IsActive)
                    {
                        World.Update(gameTime);
                    }
                }
            
        }


        /// <summary>
        /// Lets the game respond to player input. Unlike the Update method,
        /// this will only be called when the gameplay screen is active.
        /// </summary>
        public override void HandleInput(InputState input)
        {
            if (input == null) throw new ArgumentNullException("input");

            int playerIndex = (int)ControllingPlayer.Value;

            KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
            GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];


            bool gamePadDisconnected = !gamePadState.IsConnected && input.GamePadWasConnected[playerIndex];
            

           
            if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
                {
                    ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
                }
                else
                {
                    World.Detetive.HandleInput(input, ControllingPlayer, World.CurrentFootstepSound);
                    checkCollisions();
                    checkTriggers();
                    
                }
                if (TransitionPosition > 0)
                {
                    ScreenManager.FadeBackBufferToBlack(255 - TransitionAlpha);
                }
            
            
        }
      

        /// <summary>
        /// Draws the gameplay screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            ScreenManager.GraphicsDevice.Clear(Color.Black);
            if (World.GameState == GameState.GAME)
            {
                World.Draw(gameTime);
                DrawUI();
            }
            
            ScreenManager.SpriteBatch.End();    
            
        }

        public void DrawUI()
        {
            Vector2 timeSize = ScreenManager.Font.MeasureString(World.Time.ToString()) / 2;
            Vector2 timePosition = new Vector2(80, 40);
            if(!World.Time.IsTimeUp)
                ScreenManager.SpriteBatch.DrawString(ScreenManager.Font, World.Time.ToString(), timePosition, Color.Red, 0f, Vector2.Zero, 0.5f, SpriteEffects.None, 0f);

        }
        #endregion

    }
}
